var QQMapWX = require('qqmap-wx-jssdk.min.js');
const md5 = require('md5.js');
var qqmapsdk = new QQMapWX({
  key: 'YIYBZ-IEGWI-P54GK-5NO2M-O5RFS-PJBMG' // 必填
});
function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  // return year+"/"+month+"/"+day;
}

function formatTimeStamp(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;

}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function __args() {
	var setting = {};
	if (arguments.length === 1 && typeof arguments[0] !== 'string') {
		setting = arguments[0];
	} else {
		setting.url = arguments[0];
		if (typeof arguments[1] === 'object') {
			setting.data = arguments[1];
			setting.success = arguments[2];
		} else {
			setting.success = arguments[1];
		}
	}
	if (setting.url.indexOf('https://') !== 0) {
   
    // setting.url = 'https://192.168.0.116' + setting.url;
    setting.url = 'https://www.ebbtcps.com' + setting.url;
	}
	return setting;
}
function __request(method, setting) {
	setting.method=method;
	setting.header={
    'Content-Type':'application/x-www-form-urlencoded'
	};
  setting.async=false;
	wx.request(setting);
}

module.exports = {
 
  qqmapsdk: qqmapsdk,
  formatTimeStamp:formatTimeStamp,
	getReq:function () {
    __request('GET', __args.apply(this, arguments));
	},
	postReq: function () {
    __request('POST', __args.apply(this, arguments));
	},
	sendTemplate: function(formId, templateData, success, fail) {
		var app = getApp();
		this.getJSON({
			url: '/WxAppApi/sendTemplate',
			data: {
				rd_session: app.rd_session,
				form_id: formId,
				data: templateData,
			},
			success: success,   // errorcode==0时发送成功
			fail: fail
		});
	}
}
